import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/good/add-good",
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/mobileLogin",
    component: () => import("@/pages/login/mobile.vue"),
    meta: {
      hidden: true
    }
  },
]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [


  // {
  //   path: "/",
  //   redirect: "/current-order",
  //   component: Layouts,
  //   name: 'order',
  //   meta: {
  //     title: "我的订单",
  //     elIcon: "Document",
  //   },
  //   children: [
  //     {
  //       path: "current-order",
  //       component: () => import("@/pages/order/current-order.vue"),
  //       name: "CurrentOrder",
  //       meta: {
  //         title: "当前订单"
  //       }
  //     },
  //     {
  //       path: "complete-order",
  //       component: () => import("@/pages/order/complete-order.vue"),
  //       name: "CompleteOrder",
  //       meta: {
  //         title: "已完成订单"
  //       }
  //     },
  //     {
  //       path: "error-order",
  //       component: () => import("@/pages/order/error-order.vue"),
  //       name: "ErrorOrder",
  //       meta: {
  //         title: "异常订单"
  //       }
  //     }
  //   ]
  // },
  {
    path: "/good",
    component: Layouts,
    meta: {
      title: "商品信息",
      elIcon: "Goods"
    },
    children: [
      {
        path: "good-list",
        component: () => import("@/pages/good/good-list.vue"),
        name: "GoodList",
        meta: {
          title: "商品列表"
        }
      },
      {
        path: "remove-good",
        component: () => import("@/pages/good/remove-good.vue"),
        name: "RemoveGood",
        meta: {
          title: "已下架商品"
        }
      },
      {
        path: "add-good",
        component: () => import("@/pages/good/add/index.vue"),
        name: "AddGood",
        meta: {
          title: "添加商品",
          hidden: true,
        }
      }
    ]
  },
  {
    path: "/mobileGood",
    meta: {
      title: "商品信息",
      elIcon: "Goods",
      hidden: true,
    },
    children: [
      {
        path: "add-good",
        component: () => import("@/pages/mobileGood/add/index.vue"),
        name: "MobileAddGood",
        meta: {
          title: "添加商品",
          hidden: true,
        }
      }
    ],
  },
  {
    path: "/point",
    component: Layouts,
    redirect: "/point/PointRecharge",
    name: 'point',
    meta: {
      title: "积分充值",
      elIcon: "CreditCard",
    },
    children: [
      {
        path: "PointRecharge",
        component: () => import("@/pages/point/PointRecharge.vue"),
        name: "PointRecharge",
        meta: {
          title: "积分充值",
          elIcon: "CreditCard",
        }
      }
    ]
  },
  {
    path: "/shop",
    component: Layouts,
    redirect: "/shop/ShopInfo",
    name: 'shop',
    meta: {
      title: "商家信息",
      elIcon: "Goods"
    },
    children: [
      {
        path: "ShopInfo",
        component: () => import("@/pages/shop/ShopInfo/index.vue"),
        name: "ShopInfo",
        meta: {
          title: "商家信息",
          elIcon: "OfficeBuilding"
        }
      }
    ]
  },
  {
    path: "/adv",
    component: Layouts,
    name: 'adv',
    redirect: "/adv/ShopAdv",
    children: [
      {
        path: "ShopAdv",
        component: () => import("@/pages/shop/ShopAdv/index.vue"),
        name: "ShopAdv",
        meta: {
          title: "店铺广告",
          elIcon: "PriceTag",
        }
      },
      {
        path: "ShopCreateAdv/:id",
        component: () => import("@/pages/shop/ShopAdv/advEdit.vue"),
        name: "ShopCreateAdv",
        meta: {
          title: "店铺广告",
          hidden: true,
        }
      }
    ]
  },

  // {
  //   path: "/extension",
  //   component: Layouts,
  //   redirect: "/extension/ExtensionStatistics",
  //   name: 'extension',
  //   children: [
  //     {
  //       path: "ExtensionStatistics",
  //       component: () => import("@/pages/extension/ExtensionStatistics.vue"),
  //       name: "ExtensionStatistics",
  //       meta: {
  //         title: "推广统计",
  //         elIcon: "SetUp",
  //       }
  //     }
  //   ]
  // },
  {
    path: "/acquisition",
    redirect: "/acquisition/AcquisitionTool",
    component: Layouts,
    name: 'acquisition',
    children: [
      {
        path: "AcquisitionTool",
        component: () => import("@/pages/acquisition/AcquisitionTool.vue"),
        name: "AcquisitionTool",
        meta: {
          title: "获客工具",
          elIcon: "Money",
        }
      }
    ]
  },


]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
